/**
 * Created by LuBangTao on 2017/3/27.
 */
var fireworkVersion9 = function (scene) {
    $(".cut-show").css("max-width", $(window).width());
    $(".cut-show").css("max-height", $(window).height());
    var myPoints = [
        new BABYLON.Vector3(0, 0, 0),
        new BABYLON.Vector3(screenLength, 0, 0),
        new BABYLON.Vector3(screenLength, 0, screenWidth),
        new BABYLON.Vector3(0, 0, screenWidth),
        new BABYLON.Vector3(0, 0, 0),
        new BABYLON.Vector3(0, screenHeight, 0),
        new BABYLON.Vector3(screenLength, screenHeight, 0),
        new BABYLON.Vector3(screenLength, screenHeight, screenWidth),
        new BABYLON.Vector3(0, screenHeight, screenWidth),
        new BABYLON.Vector3(0, screenHeight, 0),
        new BABYLON.Vector3(0, 0, 0),
        new BABYLON.Vector3(0, 0, screenWidth),
        new BABYLON.Vector3(0, screenHeight, screenWidth),
        new BABYLON.Vector3(screenLength, screenHeight, screenWidth),
        new BABYLON.Vector3(screenLength, 0, screenWidth),
        new BABYLON.Vector3(screenLength, 0, 0),
        new BABYLON.Vector3(screenLength, screenHeight, 0),
    ];
    secondCanvas.width = screenLength * screenWidth;
    secondCanvas.height = screenHeight;
    currentFireCount++;
    var thisFireCount = currentFireCount;
    var lines = BABYLON.MeshBuilder.CreateLines(thisFireCount + "lines", {points: myPoints, updatable: true}, scene);
    if (countCapacity < maxCapacity) {
        countCapacity++;
    } else {
        Lighting.sendMessageToServer(0);
    }
    var startSphere = new BABYLON.Mesh.CreateSphere(thisFireCount + "Shoot", 4, 1, scene);
    startSphere.position.x = screenLength / 2;
    startSphere.position.z = screenWidth / 2;
    startSphere.isVisible = false;
    var particleSystem = new BABYLON.ParticleSystem(thisFireCount + "particles", 20, scene);
    particleSystem.particleTexture = new BABYLON.Texture("texture/flare.png", scene);
    particleSystem.emitter = startSphere; // the starting object, the emitter
    particleSystem.minEmitBox = new BABYLON.Vector3(0, 0, 0); // Starting all from
    particleSystem.maxEmitBox = new BABYLON.Vector3(0, 0, 0); // To...
    particleSystem.color1 = new BABYLON.Color4(.5, .5, .5, 1.0);
    particleSystem.color2 = new BABYLON.Color4(.5, .5, .5, 1.0);
    particleSystem.colorDead = new BABYLON.Color4(0, 0, 0, 0);
    particleSystem.minSize = 1.0;
    particleSystem.maxSize = 1.0;
    particleSystem.minLifeTime = .25;
    particleSystem.maxLifeTime = .25;
    particleSystem.emitRate = 50;
    particleSystem.blendMode = BABYLON.ParticleSystem.BLENDMODE_ONEONE;
    particleSystem.minEmitPower = 1;
    particleSystem.maxEmitPower = 1;
    particleSystem.updateSpeed = 0.005;
    var bigEnough = false;
    var updateFunction = function (particles) {
        for (var index = 0; index < particles.length; index++) {
            var particle = particles[index];
            particle.age += this._scaledUpdateSpeed;
            if (particle.age >= particle.lifeTime) {
                this.recycleParticle(particle);
                index--;
                continue;
            }
            else {
                if (!bigEnough) {
                    particle.size -= .02;
                }
                Lighting.drawFireworkData(particle.position.x, particle.position.y, particle.position.z, screenHeight, screenLength, secondCanvas.getContext("2d"),particle.color)
                particle.direction.scaleToRef(particleSystem._scaledUpdateSpeed, particleSystem._scaledDirection);
                particle.position.addInPlace(particleSystem._scaledDirection);
                particleSystem.gravity.scaleToRef(particleSystem._scaledUpdateSpeed, particleSystem._scaledGravity);
                particle.direction.addInPlace(particleSystem._scaledGravity);
            }
        }
    };
    particleSystem.updateFunction = updateFunction;
    particleSystem.start();
    var isTop2 = false;
    var isFired2 = false;
    scene.registerBeforeRender(function () {
        secondCanvas.width = secondCanvas.width;
        if (!isFired2) {
            if (!isTop2) {
                startSphere.position.y += .1;
                if (startSphere.position.y > screenHeight * 2 / 3) {
                    isTop2 = !isTop2;
                    particleSystem.stop();
                }
            } else {
                setTimeout(function () {
                    fire(scene);
                }, 1000);
                isFired2 = !isFired2;
            }
        }
    });

    function fire() {
        var tempFire = [];
        var tempGizmo = [];
        var sphere = BABYLON.Mesh.CreateSphere(thisFireCount +"sphere1", 8, 2, scene);
        sphere.position.x = screenLength/2;
        sphere.position.y = screenHeight * 2 / 3;
        sphere.position.z = screenWidth/2;
        sphere.visibility = 0;
        var perticleFromVerticesEmitter = sphere;
        perticleFromVerticesEmitter.useVertexColors = true;
        var verticesPositions = perticleFromVerticesEmitter.getVerticesData(BABYLON.VertexBuffer.PositionKind);
        var verticesNormals = perticleFromVerticesEmitter.getVerticesData(BABYLON.VertexBuffer.NormalKind);
        var verticesColor = [];
        for (var i = 0; i < verticesPositions.length; i += 3){
            var vertexPosition = new BABYLON.Vector3(
                verticesPositions[i],
                verticesPositions[i + 1],
                verticesPositions[i + 2]
            );
            var vertexNormal = new BABYLON.Vector3(
                verticesNormals[i],
                verticesNormals[i + 1],
                verticesNormals[i + 2]
            );
            var r = Math.random();
            var g = Math.random();
            var b = Math.random();
            var alpha = 1;
            var color = new BABYLON.Color4(r, g, b, alpha);
            verticesColor.push(r);
            verticesColor.push(g);
            verticesColor.push(b);
            verticesColor.push(alpha);

            var gizmo = BABYLON.Mesh.CreateBox('gizmo', 0.001, scene);
            gizmo.position = vertexPosition;
            gizmo.parent = perticleFromVerticesEmitter;
            var tempP =createParticleSystem(
                gizmo,
                vertexNormal.normalize().scale(10),
                color
            );
            tempFire.push(tempP);
        }
        perticleFromVerticesEmitter.setVerticesData(BABYLON.VertexBuffer.ColorKind, verticesColor);
        var isDone1 = false;
        var isDispose1 = false;
        var isSend1 = false;
        function createParticleSystem(emitter, direction, color) {
            var updateFunction = function (particles) {
                for (var index = 0; index < particles.length; index++) {
                    var particle = particles[index];
                    particle.age += this._scaledUpdateSpeed;
                    if (particle.age >= particle.lifeTime) {
                        this.recycleParticle(particle);
                        index--;
                        continue;
                    }
                    else {
                        Lighting.drawFireworkData(particle.position.x, particle.position.y, particle.position.z, screenHeight, screenLength, secondCanvas.getContext("2d"),particle.color);
                        particle.direction.scaleToRef(particleSystem._scaledUpdateSpeed, particleSystem._scaledDirection);
                        particle.position.addInPlace(particleSystem._scaledDirection);
                        particleSystem.gravity.scaleToRef(particleSystem._scaledUpdateSpeed, particleSystem._scaledGravity);
                        particle.direction.addInPlace(particleSystem._scaledGravity);
                    }
                }
            };
            var particleSystem = new BABYLON.ParticleSystem(thisFireCount +"particles", 500, scene);
            particleSystem.particleTexture = new BABYLON.Texture("texture/flare.png", scene);
            particleSystem.emitter = emitter; // the starting object, the emitter
            particleSystem.minEmitBox = new BABYLON.Vector3(0, 0, 0); // Starting all from
            particleSystem.maxEmitBox = new BABYLON.Vector3(0, 0, 0); // To...
            particleSystem.updateFunction =updateFunction;
            particleSystem.color1 = color;
            particleSystem.color2 = color;
            particleSystem.colorDead = new BABYLON.Color4(1,1, 1, 0.0);
            particleSystem.minSize = 0.1;
            particleSystem.maxSize = 0.4;
            particleSystem.minLifeTime = .3 + Math.floor(screenHeight / 10) * .15;
            particleSystem.maxLifeTime = particleSystem.minLifeTime;
            particleSystem.emitRate = 100;
            particleSystem.blendMode = BABYLON.ParticleSystem.BLENDMODE_ONEONE;
            particleSystem.gravity = new BABYLON.Vector3(0, -40, 0);
            particleSystem.direction1 = direction;
            particleSystem.direction2 = direction;
            particleSystem.minAngularSpeed = 0;
            particleSystem.maxAngularSpeed = Math.PI;
            particleSystem.minEmitPower = 1;
            particleSystem.maxEmitPower = 1;
            particleSystem.updateSpeed = 0.005;
            return particleSystem;
        }
        var timer1 = 0;
        scene.registerBeforeRender(function () {
            perticleFromVerticesEmitter.rotation.y += .02;
            if (!isSend1) {
                for (var i = 0; i < tempFire.length; i++) {
                    tempFire[i].start();
                }
                isSend1 = !isSend1;
            }
            if (!isDone1) {
                if (timer1 < screenHeight / 3) {
                    timer1 += 0.1
                } else {
                    for (var i = 0; i < tempFire.length; i++) {
                        tempFire[i].stop();
                    }
                    isDone1 = !isDone1;
                }
            } else {
                if (!isDispose1) {
                    timer1 += 0.1;
                    if (timer1 > screenHeight ) {
                        startSphere.dispose();
                        sphere.dispose();
                        for(var i =0;i<tempGizmo.length;i++){
                            tempGizmo[i].dispose();
                        }
                        for (var i = 0; i < scene.meshes.length; i++) {
                            if (scene.meshes[i].id.indexOf(thisFireCount.toString()) >= 0) {
                                scene.meshes[i].dispose();
                            }
                        }
                        countCapacity--;
                        isDispose1 = !isDispose1;
                        timer1 = 0;
                    }
                }
            }
        })
    }

};
